package bravo.xc.com.xc_bravo_android.operation.transaction;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.CommonBeans;
import bravo.xc.com.xc_bravo_android.beans.TransactionBeans;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TransactionActivity extends BaseMVPActivity<TransactionContract.TransactionView,TransactionContract.TransactionPresenter> implements TransactionContract.TransactionView,View.OnClickListener{

    private LinearLayout layout_title_left;//返回
    private TextView tv_title_msg;//标题
    private EditText redeemCode;
    private Button redeem;
    private TextView TodayTotal;
    private TextView AllTotal;
    private ProgressDialog progressDialog;
    private TransactionBeans transactionBeans;
    private CommonBeans commonBeans;


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_title_left:
                finish();
                break;
            case R.id.btn_trans_exchange:
                if (checkNull()){
                    showProgress(true);
                    transactionBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
                    transactionBeans.setExchangeCode(redeemCode.getText().toString().trim());
                    presenter.doRedeem(transactionBeans);
                    redeemCode.setText("");
                }
                break;
        }
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(TransactionActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected TransactionContract.TransactionPresenter createPresnter() {
        return new TransactionPresenter();
    }

    @Override
    protected void initView() {
        redeemCode = (EditText) findViewById(R.id.et_trans_input_redeemCode);
        redeem = (Button) findViewById(R.id.btn_trans_exchange) ;
        TodayTotal = (TextView) findViewById(R.id.tv_trans_today_income);
        AllTotal = (TextView) findViewById(R.id.tv_trans_gross_income);
        tv_title_msg=(TextView)findViewById(R.id.tv_title_msg);
        tv_title_msg.setText("交易信息");
        layout_title_left=(LinearLayout) findViewById(R.id.layout_title_left);

        progressDialog = new ProgressDialog(this,R.style.MyDialogStyle);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCanceledOnTouchOutside(false);

        layout_title_left.setOnClickListener(this);
        redeem.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        transactionBeans = new TransactionBeans();
        commonBeans = new CommonBeans();
        commonBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
        presenter.doGeTrade(commonBeans);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_transaction;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean checkNull() {
        if(redeemCode.getText().toString().trim().isEmpty()){
            showToast("请输入兑换码");
            return false;
        }
        return true;
    }

    @Override
    public void showProgress(boolean show) {

        if(progressDialog.isShowing()){
            progressDialog.dismiss();
            Log.e("@@showProgress","isShowing:dismiss");
        }
        if(show){
            progressDialog.show();
        }else {
            progressDialog.dismiss();
            Log.e("@@showProgress","dismiss");
        }

    }

    @Override
    public void setTradeData(float today, float total) {
        AllTotal.setText("总收入:"+String.valueOf(total)+"元");
        TodayTotal.setText("今日收入:"+String.valueOf(today)+"元");

    }
}
